home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Graphics Plus
/
Graphics Plus.iso
/
msdos
/
raytrace
/
pov
/
gen
/
accel
/
accel.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-16
|
8KB
|
364 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
typedef struct
{
double x;
double y;
double z;
} Vector;
char *KeyWords[] =
{
"ACCELERATION","FRAMESPERSECOND","TOTALFRAMES","STARTPOSITION",
"STARTVELOCITY","BOUNCEX","BOUNCEY","BOUNCEZ","ACCELP"
};
enum KeyTokens
{
ACCEL_TOK,FPS_TOK,TOTFRAMES_TOK,STARTPOS_TOK,STARTVEL_TOK,
BOUNCEX_TOK,BOUNCEY_TOK,BOUNCEZ_TOK,ACCELP_TOK,ERROR_TOK
};
double VectorLength(Vector *CurVector);
void VectorAdd(Vector *Vector1, Vector *Vector2);
void VectorMultiply(Vector *Vector1, double Factor);
void VectorMake(Vector *Direction, double Length);
void VectorCopy(Vector *Vector1, Vector *Vector2);
int FindToken(char *TokString);
Vector CurrentAcceleration;
Vector CurrentVelocity;
Vector CurrentPosition;
Vector AccelerationVector;
Vector PointAccelVector;
double BounceY,BounceX,BounceZ,BounceXFac,BounceYFac,BounceZFac;
int DoBounceX,DoBounceY,DoBounceZ;
int InitBounceX,InitBounceY,InitBounceZ;
double Acceleration,PointAccel,PointAccelRad,FramesPerSecond;
int TotalFrames, CurrentFrame,SkipFrames;
main(int argc, char *argv[])
{
Vector TempVector1,TempVector2;
PointAccelRad = 0.0;
if (!LoadScript(argv[1]))
{
printf("; Unable to load script %s\n",argv[1]);
exit(1);
}
SkipFrames = 0;
if (argc > 2)
SkipFrames = atoi(argv[2]);
if (!SkipFrames)
SkipFrames = 1;
VectorCopy(&CurrentAcceleration,&AccelerationVector);
printf("; Generated by ACCEL, by John M. Trindle June 1991\n");
printf("%lf %lf %lf\n",CurrentPosition.x,
CurrentPosition.y,
CurrentPosition.z);
for (CurrentFrame = 1; CurrentFrame < TotalFrames*SkipFrames; CurrentFrame++)
{
if (PointAccelRad != 0.0)
{
CreateCurrentAccel();
}
VectorCopy(&TempVector1,&CurrentVelocity);
VectorCopy(&TempVector2,&CurrentAcceleration);
VectorMultiply(&TempVector2,1.0/FramesPerSecond);
VectorAdd(&CurrentVelocity,&TempVector2);
VectorCopy(&TempVector2,&CurrentVelocity);
VectorAdd(&TempVector1,&TempVector2);
VectorMultiply(&TempVector1,0.5/FramesPerSecond);
VectorAdd(&CurrentPosition,&TempVector1);
if (DoBounceX)
{
if (BounceNumX() != InitBounceX)
{
CurrentPosition.x = BounceX;
CurrentVelocity.x = -CurrentVelocity.x*BounceXFac;
}
}
if (DoBounceY)
{
if (BounceNumY() != InitBounceY)
{
CurrentPosition.y = BounceY;
CurrentVelocity.y = -CurrentVelocity.y*BounceYFac;
}
}
if (DoBounceZ)
{
if (BounceNumZ() != InitBounceZ)
{
CurrentPosition.z = BounceZ;
CurrentVelocity.z = -CurrentVelocity.z*BounceZFac;
}
}
if (!(CurrentFrame % SkipFrames))
{
printf("%lf %lf %lf\n",CurrentPosition.x,
CurrentPosition.y,
CurrentPosition.z);
}
}
}
double VectorLength(Vector *CurVector)
{
double RetVal;
RetVal = sqrt(CurVector->x*CurVector->x+
CurVector->y*CurVector->y+
CurVector->z*CurVector->z);
return(RetVal);
}
void VectorAdd(Vector *Vector1, Vector *Vector2)
{
Vector1->x += Vector2->x;
Vector1->y += Vector2->y;
Vector1->z += Vector2->z;
}
void VectorMake(Vector *Direction, double Length)
{
double DirLength;
DirLength = VectorLength(Direction);
Direction->x *= (Length/DirLength);
Direction->y *= (Length/DirLength);
Direction->z *= (Length/DirLength);
}
void VectorCopy(Vector *Vector1, Vector *Vector2)
{
Vector1->x = Vector2->x;
Vector1->y = Vector2->y;
Vector1->z = Vector2->z;
}
void VectorMultiply(Vector *Vector1, double Factor)
{
Vector1->x *= (Factor);
Vector1->y *= (Factor);
Vector1->z *= (Factor);
}
int LoadScript(char *ScriptName)
{
FILE *ScrFP;
char *Token,*ParamString,LineBuffer[132];
int CurTok;
DoBounceX = DoBounceY = DoBounceZ = FALSE;
ScrFP = fopen(ScriptName,"r");
if (ScrFP == NULL)
return(FALSE);
while (!feof(ScrFP))
{
fgets(LineBuffer,132,ScrFP);
if (LineBuffer[strlen(LineBuffer)-1] == '\n')
LineBuffer[strlen(LineBuffer)-1] = '\0';
if (*LineBuffer == ';')
continue;
Token = strtok(LineBuffer," \t");
ParamString = LineBuffer+strlen(Token)+1;
CurTok = FindToken(Token);
switch(CurTok)
{
case ACCEL_TOK:
sscanf(ParamString,"%lf %lf %lf %lf",&Acceleration,
&(AccelerationVector.x),
&(AccelerationVector.y),
&(AccelerationVector.z));
VectorMake(&AccelerationVector,Acceleration);
break;
case FPS_TOK:
sscanf(ParamString,"%lf",&FramesPerSecond);
break;
case TOTFRAMES_TOK:
sscanf(ParamString,"%d",&TotalFrames);
break;
case STARTPOS_TOK:
sscanf(ParamString,"%lf %lf %lf",
&(CurrentPosition.x),
&(CurrentPosition.y),
&(CurrentPosition.z));
break;
case STARTVEL_TOK:
sscanf(ParamString,"%lf %lf %lf",
&(CurrentVelocity.x),
&(CurrentVelocity.y),
&(CurrentVelocity.z));
break;
case BOUNCEX_TOK:
DoBounceX = TRUE;
sscanf(ParamString,"%lf %lf",&BounceX,&BounceXFac);
break;
case BOUNCEY_TOK:
DoBounceY = TRUE;
sscanf(ParamString,"%lf %lf",&BounceY,&BounceYFac);
break;
case BOUNCEZ_TOK:
DoBounceZ = TRUE;
sscanf(ParamString,"%lf %lf",&BounceZ,&BounceZFac);
break;
case ACCELP_TOK:
sscanf(ParamString,"%lf %lf %lf %lf %lf",&PointAccel,&PointAccelRad,
&(PointAccelVector.x),
&(PointAccelVector.y),
&(PointAccelVector.z));
break;
}
}
if (DoBounceX)
InitBounceX = BounceNumX();
if (DoBounceY)
InitBounceY = BounceNumY();
if (DoBounceZ)
InitBounceZ = BounceNumZ();
fclose(ScrFP);
return(TRUE);
}
int FindToken(char *TokString)
{
int RetVal,i;
for (i = 0; i < ERROR_TOK; i++)
{
if (!stricmp(TokString,KeyWords[i]))
break;
}
RetVal = i;
return(RetVal);
}
int BounceNumX()
{
int RetVal;
if (CurrentPosition.x > BounceX)
RetVal = 1;
if (CurrentPosition.x < BounceX)
RetVal = 0;
if (CurrentPosition.x == BounceX)
RetVal = -1;
return(RetVal);
}
int BounceNumY()
{
int RetVal;
if (CurrentPosition.y > BounceY)
RetVal = 1;
if (CurrentPosition.y < BounceY)
RetVal = 0;
if (CurrentPosition.y == BounceY)
RetVal = -1;
return(RetVal);
}
int BounceNumZ()
{
int RetVal;
if (CurrentPosition.z > BounceZ)
RetVal = 1;
if (CurrentPosition.z < BounceZ)
RetVal = 0;
if (CurrentPosition.z == BounceZ)
RetVal = -1;
return(RetVal);
}
CreateCurrentAccel()
{
double Distance,AccelMag;
CurrentAcceleration.x = PointAccelVector.x - CurrentPosition.x;
CurrentAcceleration.y = PointAccelVector.y - CurrentPosition.y;
CurrentAcceleration.z = PointAccelVector.z - CurrentPosition.z;
Distance = VectorLength(&CurrentAcceleration);
if (Distance > 0.0)
AccelMag = (PointAccelRad/Distance)*(PointAccelRad/Distance);
else
AccelMag = 0.0;
AccelMag *= PointAccel;
VectorMake(&CurrentAcceleration,AccelMag);
VectorAdd(&CurrentAcceleration,&AccelerationVector);
}